﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmMarkWork : DockContent
    {
        int _EmployeeID = 0;
        int _BranchID = 0;
        int _DepartmentID = 0;
        frmProgressbar fProgress;
        MarkWorkClient _wcfMarkWork;
        CheckTimeClient _wcfCheckTime;
        EmployeeShiftClient _wcfEmployeeShift;
        EmployeeShiftInfo _ESInfo;
        ShiftClient _wcfShift;
        ShiftInfo _ShiftInfo;
        MarkWorkInfo _MWInfo;
        private ToolStripControlHost dtTScomponent;

        public frmMarkWork()
        {
            InitializeComponent();
            LoadLanguage();
            _wcfMarkWork = Service.MarkWork();
            _wcfEmployeeShift = Service.EmployeeShift();
            _wcfCheckTime = Service.CheckTime();
            _wcfShift = Service.Shift();

            _ShiftInfo = new ShiftInfo();
            //listRole = UIParamater.Quyen_BangChamCong);
            UISetting.LoadTreeView3(ref tvBranch);
            dtpdate.Value = DateTime.Today;
            //dtTScomponent = new ToolStripControlHost(dtpdate);
            //toolStrip1.Items.Add(dtTScomponent);

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            mnuCellEdit.Text = UpdateUI.GetString(mnuCellEdit.Tag.ToString());
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                _BranchID = 0;
                _DepartmentID = 0;
                _EmployeeID = 0;
            }
            if (e.Node.Level == 1)
            {
                _BranchID = Convert.ToInt32(e.Node.Tag);
                _DepartmentID = 0;
            }
            if (e.Node.Level >= 2)
            {
                _DepartmentID = Convert.ToInt32(e.Node.Tag);
                _BranchID = Convert.ToInt32(GetBranchNode(e.Node).Tag);
                OpenSplash();
            }
        }

        TreeNode GetBranchNode(TreeNode node)
        {
            TreeNode retVal = node;
            if (node.Level >= 2)
                retVal = node.Parent;
            return retVal;
        }

        private class MyHeader : SourceGrid.Cells.ColumnHeader
        {
            public MyHeader(object value)
                : base(value)
            {
                //1 Header Row
                SourceGrid.Cells.Views.ColumnHeader view = new SourceGrid.Cells.Views.ColumnHeader();
                view.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
                view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
                View = view;
                AutomaticSortEnabled = false;
            }
        }

        void bgJ_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = LoadCheckList();
        }

        void bgJ_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            fProgress.Close();
        }

        private void OpenSplash()
        {
            fProgress = new frmProgressbar();
            fProgress.Show();
            fProgress.Refresh();
            Application.DoEvents();
            LoadCheckList();
            fProgress.Close();
        }

        private bool LoadCheckList()
        {
            DateTime _dateVal = dtpdate.Value;
            dgvList.Rows.Clear();

            //chuyển về ngày đầu tuần
            _dateVal = _dateVal.AddDays(1 - Convert.ToInt16(_dateVal.DayOfWeek.ToString("D")));
            dtpdate.Value = _dateVal;

            //Đếm số ca có trong 1 phòng ban:
            int _shiftCount = 0;
            DataTable _dtShift = _wcfShift.Shift_GetAllByDepartment(_DepartmentID).Tables[0];
            DataRow[] _drShift = _dtShift.Select("Shift_TimeIn<>''");
            _shiftCount = _drShift.Length;

            _ESInfo = new EmployeeShiftInfo();
            DataTable dtCheckTime = _wcfCheckTime.CheckTime_GetAll(_DepartmentID, _dateVal.AddDays(-1), _dateVal.AddDays(8)).Tables[0];// _wcfCheckTime.CheckTime_GetList(out itemCount, currPage, pageSize, _BranchID, _DepartmentID, _month, _year).Tables[0];
            DataTable dtEmployee = _wcfEmployeeShift.EmployeeShift_GetEmployee(_DepartmentID).Tables[0];
            if (_shiftCount > 5)
                SourceGrid.Grid.MaxSpan = _shiftCount;

            dgvList.Redim(dtEmployee.Rows.Count + 2, 7 * _shiftCount + 1);
            dgvList.FixedRows = 2;
            SourceGrid.Cells.Views.Cell _ViewQuetSai = new SourceGrid.Cells.Views.Cell();
            SourceGrid.Cells.Views.Cell _ViewDungGio = new SourceGrid.Cells.Views.Cell();
            SourceGrid.Cells.Views.Cell _ViewVeSom = new SourceGrid.Cells.Views.Cell();
            SourceGrid.Cells.Views.Cell _ViewDiMuon = new SourceGrid.Cells.Views.Cell();
            SourceGrid.Cells.Views.Cell _ViewKhongQuet = new SourceGrid.Cells.Views.Cell();
            SourceGrid.Cells.Views.Cell _ViewDiMuonVeSom = new SourceGrid.Cells.Views.Cell();

            //Tô màu:
            _ViewQuetSai.BackColor = Color.Red; //Quẹt sai
            _ViewDiMuonVeSom.BackColor = Color.DarkOrange;
            _ViewDungGio.BackColor = Color.LightGreen; //Đúng giờ
            _ViewVeSom.BackColor = Color.Yellow; //Về sớm
            _ViewDiMuon.BackColor = Color.Violet; //Muộn
            _ViewKhongQuet.BackColor = Color.Gray; //Không đi làm

            dgvList[0, 0] = new MyHeader(UpdateUI.GetString("nhanvien"));
            dgvList[0, 0].RowSpan = 2;

            for (int i = 1; i < dgvList.Columns.Count; i = i + _shiftCount)
            {
                dgvList[0, 1] = new MyHeader(UISetting.GetDayOfWeek(_dateVal) + "\n" + _dateVal.ToString("dd/MM/yyyy"));
                dgvList[0, 1].ColumnSpan = _shiftCount;

                dgvList[0, 1 + _shiftCount] = new MyHeader(UISetting.GetDayOfWeek(_dateVal.AddDays(1)) + "\n" + _dateVal.AddDays(1).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + _shiftCount].ColumnSpan = _shiftCount;

                dgvList[0, 1 + 2 * _shiftCount] = new MyHeader(UISetting.GetDayOfWeek(_dateVal.AddDays(2)) + "\n" + _dateVal.AddDays(2).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 2 * _shiftCount].ColumnSpan = _shiftCount;

                dgvList[0, 1 + 3 * _shiftCount] = new MyHeader(UISetting.GetDayOfWeek(_dateVal.AddDays(3)) + "\n" + _dateVal.AddDays(3).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 3 * _shiftCount].ColumnSpan = _shiftCount;

                dgvList[0, 1 + 4 * _shiftCount] = new MyHeader(UISetting.GetDayOfWeek(_dateVal.AddDays(4)) + "\n" + _dateVal.AddDays(4).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 4 * _shiftCount].ColumnSpan = _shiftCount;

                dgvList[0, 1 + 5 * _shiftCount] = new MyHeader(UISetting.GetDayOfWeek(_dateVal.AddDays(5)) + "\n" + _dateVal.AddDays(5).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 5 * _shiftCount].ColumnSpan = _shiftCount;

                dgvList[0, 1 + 6 * _shiftCount] = new MyHeader(UISetting.GetDayOfWeek(_dateVal.AddDays(6)) + "\n" + _dateVal.AddDays(6).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 6 * _shiftCount].ColumnSpan = _shiftCount;

            }

            //Đặt tên ca cho header của datagrid:
            for (int i = 1; i < dgvList.Columns.Count; i = i + _shiftCount)
            {
                for (int j = 0; j < _shiftCount; j++)
                {
                    dgvList[1, i + j] = new MyHeader(_drShift[j]["Shift_Name"].ToString());
                    dgvList[1, i + j].Tag = Convert.ToInt32(_drShift[j]["Shift_ID"]);
                }
            }

            //vẽ grid
            for (int i = 0; i < dtEmployee.Rows.Count; i++)
            {
                dgvList[i + 2, 0] = new SourceGrid.Cells.Cell(dtEmployee.Rows[i]["FullName"].ToString(), typeof(string));
                dgvList[i + 2, 0].Tag = dtEmployee.Rows[i]["Employee_CardNo"].ToString();
                for (int j = 1; j < dgvList.Columns.Count; j++)
                {
                    dgvList[i + 2, j] = new SourceGrid.Cells.Cell();
                }
            }
            string[] _CheckDate = null;
            //Hiển thị phân ca, và các thời điểm check in-out trong ca
            for (int i = 0; i < dtEmployee.Rows.Count; i++)
            {
                _EmployeeID = Convert.ToInt32(dtEmployee.Rows[i]["Employee_ID"]);
                for (int j = 1; j < dgvList.Columns.Count; j++)
                {
                    #region Duyệt qua từng cột(ca) trong grid
                    int _temp = (j - 1) / _shiftCount;
                    DateTime _currDate = Convert.ToDateTime(_dateVal.AddDays(_temp));

                    //Lấy thông tin phân ca
                    _ESInfo = _wcfEmployeeShift.EmployeeShift_GetShift(_EmployeeID, _currDate);
                    if (_ESInfo != null)
                        if (_ESInfo.ShiftID == Convert.ToInt32(dgvList[1, j].Tag))
                        {
                            dgvList[i + 2, j].ToolTipText = _ESInfo.ShiftID.ToString();
                            dgvList[i + 2, j].View = _ViewKhongQuet;//Đánh dấu ca được phân
                            DataRow[] drShift = _dtShift.Select("Shift_ID=" + _ESInfo.ShiftID.ToString());

                            if (drShift.Length > 0)
                            {
                                _ShiftInfo = GetShift(drShift[0]);
                                if (_ShiftInfo.TimeIn != "")
                                    foreach (DataRow dr in dtCheckTime.Rows)
                                    {
                                        #region Duyệt qua từng thời điểm quẹt thẻ
                                        if (dr["Check_Status"].ToString() != "1")//Đã check rồi thì bỏ qua
                                        {
                                            DateTime _TimeIn = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeIn, "dd/MM/yyyy HH:mm", null);
                                            DateTime _TimeOut = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOut, "dd/MM/yyyy HH:mm", null);
                                            if (DateTime.Compare(_TimeIn, _TimeOut) > 0)
                                                _TimeOut = _TimeOut.AddDays(1);

                                            DateTime _TimeInB = DateTime.MinValue;
                                            DateTime _TimeOutB = DateTime.MinValue;
                                            if (_ShiftInfo.TimeInB != "")
                                                _TimeInB = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeInB, "dd/MM/yyyy HH:mm", null);
                                            if (_ShiftInfo.TimeInB != "")
                                                _TimeOutB = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOutB, "dd/MM/yyyy HH:mm", null);
                                            if (DateTime.Compare(_TimeInB, _TimeOutB) > 0)
                                                _TimeOutB = _TimeOutB.AddDays(1);


                                            //So sánh giờ ra giờ vào:
                                            if (_ShiftInfo.TimeInB == "")//Ca thường
                                            {
                                                if (Convert.ToInt32(dr["Employee_ID"]) == _EmployeeID
                                                    && DateTime.Compare(_TimeIn.AddHours(-5), Convert.ToDateTime(dr["Check_Date"])) < 0
                                                    && DateTime.Compare(_TimeOut.AddHours(8), Convert.ToDateTime(dr["Check_Date"])) > 0)
                                                {
                                                    dr["Check_Status"] = "1";//Đánh dấu đã check
                                                    //Điền dữ liệu quẹt thẻ:
                                                    if (dgvList[i + 2, j].Value != null)
                                                    {
                                                        dgvList[i + 2, j].Value = dgvList[i + 2, j].Value + " - " + Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                        dgvList[i + 2, j].Tag = dgvList[i + 2, j].Tag + "," + dr["Check_ID"].ToString();
                                                    }
                                                    else
                                                    {
                                                        dgvList[i + 2, j].Value = Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                        dgvList[i + 2, j].Tag = dr["Check_ID"].ToString();
                                                    }
                                                }
                                            }
                                            else//Ca gẫy - chỉ so sánh giờ vào đầu tiên và ra cuối cùng
                                            {
                                                if (Convert.ToInt32(dr["Employee_ID"]) == _EmployeeID
                                                   && (DateTime.Compare(_TimeIn.AddHours(-5), Convert.ToDateTime(dr["Check_Date"])) < 0
                                                   && DateTime.Compare(_TimeOutB.AddHours(8), Convert.ToDateTime(dr["Check_Date"])) > 0))
                                                {
                                                    dr["Check_Status"] = "1";//Đánh dấu đã check
                                                    //Điền dữ liệu quẹt thẻ:
                                                    if (dgvList[i + 2, j].Value != null)
                                                    {
                                                        dgvList[i + 2, j].Value = dgvList[i + 2, j].Value + " - " + Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                        dgvList[i + 2, j].Tag = dgvList[i + 2, j].Tag + "," + dr["Check_ID"].ToString();
                                                    }
                                                    else
                                                    {
                                                        dgvList[i + 2, j].Value = Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                        dgvList[i + 2, j].Tag = dr["Check_ID"].ToString();
                                                    }
                                                }
                                            }
                                        }
                                        #endregion
                                    }

                            }
                        }
                    #endregion
                }
            }

            //Hiển thị các thời điểm check in-out khác
            for (int i = 0; i < dtEmployee.Rows.Count; i++)
            {
                _EmployeeID = Convert.ToInt32(dtEmployee.Rows[i]["Employee_ID"]);
                for (int j = 1; j < dgvList.Columns.Count; j++)
                {

                    #region Duyệt qua từng cột(ca) trong grid
                    int _temp = (j - 1) / _shiftCount;
                    DateTime _currDate = Convert.ToDateTime(_dateVal.AddDays(_temp));
                    DataRow[] drShift = _dtShift.Select("Shift_ID=" + dgvList[1, j].Tag.ToString());
                    if (drShift.Length > 0)
                    {
                        _ShiftInfo = GetShift(drShift[0]);
                        if (_ShiftInfo.TimeIn != "")
                            foreach (DataRow dr in dtCheckTime.Rows)
                            {

                                #region Duyệt qua từng thời điểm quẹt thẻ
                                if (dr["Check_Status"].ToString() != "1")//Đã check rồi thì bỏ qua
                                {
                                    DateTime _TimeIn = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeIn, "dd/MM/yyyy HH:mm", null);
                                    DateTime _TimeOut = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOut, "dd/MM/yyyy HH:mm", null);
                                    if (DateTime.Compare(_TimeIn, _TimeOut) > 0)
                                        _TimeOut = _TimeOut.AddDays(1);

                                    DateTime _TimeInB = DateTime.MinValue;
                                    DateTime _TimeOutB = DateTime.MinValue;
                                    if (_ShiftInfo.TimeInB != "")
                                        _TimeInB = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeInB, "dd/MM/yyyy HH:mm", null);
                                    if (_ShiftInfo.TimeInB != "")
                                        _TimeOutB = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOutB, "dd/MM/yyyy HH:mm", null);
                                    if (DateTime.Compare(_TimeInB, _TimeOutB) > 0)
                                        _TimeOutB = _TimeOutB.AddDays(1);


                                    //So sánh giờ ra giờ vào:
                                    if (_ShiftInfo.TimeInB == "")//Ca thường
                                    {
                                        if (Convert.ToInt32(dr["Employee_ID"]) == _EmployeeID
                                            && DateTime.Compare(_TimeIn.AddHours(-5), Convert.ToDateTime(dr["Check_Date"])) < 0
                                            && DateTime.Compare(_TimeOut.AddHours(8), Convert.ToDateTime(dr["Check_Date"])) > 0)
                                        {
                                            dr["Check_Status"] = "1";//Đánh dấu đã check
                                            //Điền dữ liệu quẹt thẻ:
                                            if (dgvList[i + 2, j].Value != null)
                                            {
                                                dgvList[i + 2, j].Value = dgvList[i + 2, j].Value + " - " + Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                dgvList[i + 2, j].Tag = dgvList[i + 2, j].Tag + "," + dr["Check_ID"].ToString();
                                            }
                                            else
                                            {
                                                dgvList[i + 2, j].Value = Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                dgvList[i + 2, j].Tag = dr["Check_ID"].ToString();
                                            }
                                        }
                                    }
                                    else//Ca gẫy - chỉ so sánh giờ vào đầu tiên và ra cuối cùng
                                    {
                                        if (Convert.ToInt32(dr["Employee_ID"]) == _EmployeeID
                                           && (DateTime.Compare(_TimeIn.AddHours(-5), Convert.ToDateTime(dr["Check_Date"])) < 0
                                           && DateTime.Compare(_TimeOutB.AddHours(8), Convert.ToDateTime(dr["Check_Date"])) > 0))
                                        {
                                            dr["Check_Status"] = "1";//Đánh dấu đã check
                                            //Điền dữ liệu quẹt thẻ:
                                            if (dgvList[i + 2, j].Value != null)
                                            {
                                                dgvList[i + 2, j].Value = dgvList[i + 2, j].Value + " - " + Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                dgvList[i + 2, j].Tag = dgvList[i + 2, j].Tag + "," + dr["Check_ID"].ToString();
                                            }
                                            else
                                            {
                                                dgvList[i + 2, j].Value = Convert.ToDateTime(dr["Check_Date"]).ToString("HH:mm");
                                                dgvList[i + 2, j].Tag = dr["Check_ID"].ToString();
                                            }
                                        }
                                    }
                                }
                                #endregion
                            }

                    }

                    #endregion
                }
            }


            DateTime _TimeIn1 = DateTime.MinValue;
            DateTime _TimeOut1 = DateTime.MinValue;
            DateTime _TimeIn2 = DateTime.MinValue;
            DateTime _TimeOut2 = DateTime.MinValue;

            DateTime _CheckIn1 = DateTime.MinValue;
            DateTime _CheckOut1 = DateTime.MinValue;
            DateTime _CheckIn2 = DateTime.MinValue;
            DateTime _CheckOut2 = DateTime.MinValue;

            //Đánh dấu đi sơm về muộn và chấm công
            for (int i = 0; i < dtEmployee.Rows.Count; i++)
            {
                _EmployeeID = Convert.ToInt32(dtEmployee.Rows[i]["Employee_ID"]);

                _MWInfo = new MarkWorkInfo();
                _MWInfo.ID = 0;
                _MWInfo.EmployeeID = _EmployeeID;



                for (int j = 1; j < dgvList.Columns.Count; j++)
                {
                    int _temp = (j - 1) / _shiftCount;
                    DateTime _currDate = Convert.ToDateTime(_dateVal.AddDays(_temp));

                    if (dgvList[i + 2, j].ToolTipText != null)//Chỉ kiểm tra ca được phân
                    {
                        DataRow[] drShift = _dtShift.Select("Shift_ID=" + dgvList[i + 2, j].ToolTipText);
                        if (drShift.Length > 0)
                        {
                            _ShiftInfo = GetShift(drShift[0]);
                            if (dgvList[i + 2, j].Value != null)
                            {
                                _CheckDate = dgvList[i + 2, j].Value.ToString().Trim().Split('-');

                                _TimeIn1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeIn, "dd/MM/yyyy HH:mm", null);
                                _TimeOut1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOut, "dd/MM/yyyy HH:mm", null);
                                if (DateTime.Compare(_TimeIn1, _TimeOut1) > 0)
                                    _TimeOut1 = _TimeOut1.AddDays(1);

                                if (_ShiftInfo.TimeInB != "")
                                {
                                    _TimeIn2 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeInB, "dd/MM/yyyy HH:mm", null);
                                    _TimeOut2 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOutB, "dd/MM/yyyy HH:mm", null);
                                    if (DateTime.Compare(_TimeIn2, _TimeOut2) > 0)
                                        _TimeOut2 = _TimeOut2.AddDays(1);
                                }

                                //Ca thường
                                if (_ShiftInfo.TimeInB == "")
                                {
                                    if (_CheckDate.Length != 2)//Quẹt thẻ ít, nhiều hơn quy định
                                        dgvList[i + 2, j].View = _ViewQuetSai;
                                    else
                                    {
                                        _CheckIn1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _CheckDate[0].Trim(), "dd/MM/yyyy HH:mm", null);
                                        _CheckOut1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _CheckDate[1].Trim(), "dd/MM/yyyy HH:mm", null);
                                        if (DateTime.Compare(_CheckIn1, _CheckOut1) > 0)
                                            _CheckOut1 = _CheckOut1.AddDays(1);

                                        if (_currDate.DayOfWeek == DayOfWeek.Saturday && _ShiftInfo.Name.ToUpper() == "OH")
                                        {
                                            _ShiftInfo.TimeOut = "12:00";
                                            _TimeOut1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _ShiftInfo.TimeOut, "dd/MM/yyyy HH:mm", null);
                                        }

                                        //Đặt giờ chênh lệch 5 phút
                                        if (DateTime.Compare(_TimeIn1.AddMinutes(5), _CheckIn1) < 0 && DateTime.Compare(_TimeOut1.AddMinutes(-5), _CheckOut1) > 0)
                                            dgvList[i + 2, j].View = _ViewDiMuonVeSom;
                                        else
                                            if (DateTime.Compare(_TimeIn1.AddMinutes(5), _CheckIn1) < 0)
                                                dgvList[i + 2, j].View = _ViewDiMuon;
                                            else
                                                if (DateTime.Compare(_TimeOut1.AddMinutes(-5), _CheckOut1) > 0)
                                                    dgvList[i + 2, j].View = _ViewVeSom;
                                                else
                                                    dgvList[i + 2, j].View = _ViewDungGio;

                                        #region Chấm công

                                        _MWInfo.CheckDate = _currDate;
                                        _MWInfo.ShiftID = _ShiftInfo.ID;
                                        _MWInfo.TimeIn = _CheckIn1;
                                        _MWInfo.TimeOut = _CheckOut1;
                                        _MWInfo.TimeInB = DateTime.MinValue;
                                        _MWInfo.TimeOutB = DateTime.MinValue;

                                        _MWInfo.HolidayTime = 0;
                                        _MWInfo.OffTime = 0;
                                        _MWInfo.OverTime = 0;
                                        _MWInfo.NightOverTime = 0;
                                        _MWInfo.NightShiftTime = 0;
                                        _MWInfo.Cong = 0;
                                        _MWInfo.WorkTime = 0;

                                        DateTime _NightStart = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " 22:00", "dd/MM/yyyy HH:mm", null);
                                        DateTime _NightEnd = DateTime.ParseExact(_currDate.AddDays(1).ToString("dd/MM/yyyy") + " 04:00", "dd/MM/yyyy HH:mm", null);

                                        #region Tính overtime
                                        TimeSpan _ts1 = _TimeIn1 - _CheckIn1;

                                        if (_ts1.TotalMinutes > 30)
                                        {
                                            if (_TimeIn1 >= _NightStart && _TimeIn1 <= _NightEnd)
                                            {
                                                if (_CheckIn1 >= _NightStart && _CheckIn1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += 0;
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_TimeIn1 - _CheckIn1).TotalHours);
                                                }
                                                else if (_CheckIn1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _CheckIn1).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_TimeIn1 - _NightStart).TotalHours);
                                                }
                                            }
                                            else if (_TimeIn1 > _NightEnd)
                                            {
                                                if (_CheckIn1 >= _NightStart && _CheckIn1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_TimeIn1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _CheckIn1).TotalHours);
                                                }
                                                else if (_CheckIn1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _CheckIn1).TotalHours + (_TimeIn1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                                }
                                                else if (_CheckIn1 < _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_TimeIn1 - _CheckIn1).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }

                                            }
                                            else if (_TimeIn1 < _NightStart)
                                            {
                                                _MWInfo.OverTime += Convert.ToDecimal((_TimeIn1 - _CheckIn1).TotalHours);
                                                _MWInfo.NightOverTime += 0;
                                            }
                                        }

                                        _ts1 = _CheckOut1 - _TimeOut1;
                                        if (_ts1.TotalMinutes > 30)
                                        {
                                            if (_CheckOut1 >= _NightStart && _CheckOut1 <= _NightEnd)
                                            {
                                                if (_TimeOut1 >= _NightStart && _TimeOut1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += 0;
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                }
                                                else if (_TimeOut1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _TimeOut1).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_CheckOut1 - _NightStart).TotalHours);
                                                }
                                                else if (_TimeOut1 > _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }
                                            }
                                            else if (_CheckOut1 > _NightEnd)
                                            {
                                                if (_TimeOut1 >= _NightStart && _TimeOut1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _TimeOut1).TotalHours);
                                                }
                                                else if (_TimeOut1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _TimeOut1).TotalHours + (_CheckOut1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                                }
                                                else if (_TimeOut1 > _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }

                                            }
                                            else if (_CheckOut1 < _NightStart)
                                            {
                                                _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                _MWInfo.NightOverTime += 0;
                                            }
                                        }
                                        #endregion

                                        #region Tính NightShift
                                        if (_TimeIn1 >= _NightStart && _TimeIn1 <= _NightEnd)
                                        {
                                            if (_TimeOut1 <= _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_TimeOut1 - _TimeIn1).TotalHours);
                                            else if (_TimeOut1 > _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_NightEnd - _TimeIn1).TotalHours);

                                        }
                                        else if (_TimeIn1 < _NightStart)
                                        {
                                            if (_TimeOut1 >= _NightStart && _TimeOut1 <= _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_TimeOut1 - _NightStart).TotalHours);
                                            else if (_TimeOut1 > _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                        }
                                        else
                                            _MWInfo.NightShiftTime = 0;
                                        #endregion

                                        #region Thời gian làm việc trong ngày

                                        //Tính giờ làm sau khi trừ đi 1 tiếng nghỉ trưa
                                        _MWInfo.WorkTime = Convert.ToDecimal((_TimeOut1 - _TimeIn1).TotalHours) - 1;

                                        #endregion

                                        #region DemCong
                                        
                                        if (_MWInfo.WorkTime > 0)
                                            _MWInfo.Cong = _MWInfo.WorkTime / 8;

                                        #endregion

                                        _MWInfo.IsWorking = true;

                                        if (!_wcfMarkWork.MarkWork_CheckExistEmployee(_EmployeeID, _currDate))
                                        {
                                            _MWInfo.ID = _wcfMarkWork.MarkWork_Insert(_MWInfo);
                                        }
                                        else
                                        {
                                            _wcfMarkWork.MarkWork_Update(_MWInfo);
                                        }
                                        #endregion
                                    }
                                }
                                #region Ca gãy
                                /*
                                else//Ca gẫy
                                {
                                    if (_CheckDate.Length != 4)
                                        dgvList[i + 2, j].View = _ViewQuetSai;
                                    else
                                    {
                                        _CheckIn1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _CheckDate[0].Trim(), "dd/MM/yyyy HH:mm", null);
                                        _CheckOut1 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _CheckDate[1].Trim(), "dd/MM/yyyy HH:mm", null);
                                        if (DateTime.Compare(_CheckIn1, _CheckOut1) > 0)
                                            _CheckOut1 = _CheckOut1.AddDays(1);

                                        _CheckIn2 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _CheckDate[2].Trim(), "dd/MM/yyyy HH:mm", null);
                                        _CheckOut2 = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " " + _CheckDate[3].Trim(), "dd/MM/yyyy HH:mm", null);
                                        if (DateTime.Compare(_CheckIn2, _CheckOut2) > 0)
                                            _CheckOut2 = _CheckOut2.AddDays(1);

                                        if ((DateTime.Compare(_TimeIn1.AddMinutes(5), _CheckIn1) < 0 && DateTime.Compare(_TimeOut1.AddMinutes(-5), _CheckOut1) > 0)
                                            || (DateTime.Compare(_TimeIn1.AddMinutes(5), _CheckIn1) < 0 && DateTime.Compare(_TimeOut2.AddMinutes(-5), _CheckOut2) > 0)
                                            || (DateTime.Compare(_TimeIn2.AddMinutes(5), _CheckIn2) < 0 && DateTime.Compare(_TimeOut2.AddMinutes(-5), _CheckOut2) > 0)
                                            || (DateTime.Compare(_TimeIn2.AddMinutes(5), _CheckIn2) < 0 && DateTime.Compare(_TimeOut1.AddMinutes(-5), _CheckOut1) > 0))
                                            dgvList[i + 2, j].View = _ViewDiMuonVeSom;
                                        else
                                            if ((DateTime.Compare(_TimeOut1.AddMinutes(-5), _CheckOut1) > 0)
                                                || (DateTime.Compare(_TimeOut2.AddMinutes(-5), _CheckOut2) > 0))
                                                dgvList[i + 2, j].View = _ViewVeSom;
                                            else
                                                if ((DateTime.Compare(_TimeIn1.AddMinutes(5), _CheckIn1) < 0)
                                                     || (DateTime.Compare(_TimeIn2.AddMinutes(5), _CheckIn2) < 0))
                                                    dgvList[i + 2, j].View = _ViewDiMuon;
                                                else
                                                    dgvList[i + 2, j].View = _ViewDungGio;
                                        
                                        #region Chấm công
                                        
                                        _MWInfo.CheckDate = _currDate;


                                        _MWInfo.ShiftID = _ShiftInfo.ID;
                                        _MWInfo.TimeIn = _CheckIn1;
                                        _MWInfo.TimeOut = _CheckOut1;
                                        _MWInfo.TimeInB = _CheckIn2;
                                        _MWInfo.TimeOutB = _CheckOut2;

                                        _MWInfo.HolidayTime = 0;
                                        _MWInfo.OffTime = 0;
                                        _MWInfo.OverTime = 0;
                                        _MWInfo.NightOverTime = 0;
                                        _MWInfo.NightShiftTime = 0;

                                        DateTime _NightStart = DateTime.ParseExact(_currDate.ToString("dd/MM/yyyy") + " 22:00", "dd/MM/yyyy HH:mm", null);
                                        DateTime _NightEnd = DateTime.ParseExact(_currDate.AddDays(1).ToString("dd/MM/yyyy") + " 04:00", "dd/MM/yyyy HH:mm", null);

                                        #region Tính cho ca gẫy 1
                                        TimeSpan _ts1 = _TimeIn1 - _CheckIn1;

                                        #region Tính Overtime
                                        if (_ts1.TotalMinutes > 30)
                                        {
                                            if (_TimeIn1 >= _NightStart && _TimeIn1 <= _NightEnd)
                                            {
                                                if (_CheckIn1 >= _NightStart && _CheckIn1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += 0;
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_TimeIn1 - _CheckIn1).TotalHours);
                                                }
                                                else if (_CheckIn1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _CheckIn1).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_TimeIn1 - _NightStart).TotalHours);
                                                }
                                            }
                                            else if (_TimeIn1 > _NightEnd)
                                            {
                                                if (_CheckIn1 >= _NightStart && _CheckIn1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_TimeIn1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _CheckIn1).TotalHours);
                                                }
                                                else if (_CheckIn1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _CheckIn1).TotalHours + (_TimeIn1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                                }
                                                else if (_CheckIn1 < _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_TimeIn1 - _CheckIn1).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }

                                            }
                                            else if (_TimeIn1 < _NightStart)
                                            {
                                                _MWInfo.OverTime += Convert.ToDecimal((_TimeIn1 - _CheckIn1).TotalHours);
                                                _MWInfo.NightOverTime += 0;
                                            }
                                        }

                                        _ts1 = _CheckOut1 - _TimeOut1;
                                        if (_ts1.TotalMinutes > 30)
                                        {
                                            if (_CheckOut1 >= _NightStart && _CheckOut1 <= _NightEnd)
                                            {
                                                if (_TimeOut1 >= _NightStart && _TimeOut1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += 0;
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                }
                                                else if (_TimeOut1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _TimeOut1).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_CheckOut1 - _NightStart).TotalHours);
                                                }
                                                else if (_TimeOut1 > _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }
                                            }
                                            else if (_CheckOut1 > _NightEnd)
                                            {
                                                if (_TimeOut1 >= _NightStart && _TimeOut1 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _TimeOut1).TotalHours);
                                                }
                                                else if (_TimeOut1 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _TimeOut1).TotalHours + (_CheckOut1 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                                }
                                                else if (_TimeOut1 > _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }

                                            }
                                            else if (_CheckOut1 < _NightStart)
                                            {
                                                _MWInfo.OverTime += Convert.ToDecimal((_CheckOut1 - _TimeOut1).TotalHours);
                                                _MWInfo.NightOverTime += 0;
                                            }
                                        }
                                        #endregion

                                        #region Tính NightShift
                                        if (_TimeIn1 >= _NightStart && _TimeIn1 <= _NightEnd)
                                        {
                                            if (_TimeOut1 <= _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_TimeOut1 - _TimeIn1).TotalHours);
                                            else if (_TimeOut1 > _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_NightEnd - _TimeIn1).TotalHours);

                                        }
                                        else if (_TimeIn1 < _NightStart)
                                        {
                                            if (_TimeOut1 >= _NightStart && _TimeOut1 <= _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_TimeOut1 - _NightStart).TotalHours);
                                            else if (_TimeOut1 > _NightEnd)
                                                _MWInfo.NightShiftTime = Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                        }
                                        else
                                            _MWInfo.NightShiftTime = 0;
                                        #endregion

                                        #endregion

                                        #region Tính cho ca gẫy 2
                                        _ts1 = _TimeIn2 - _CheckIn2;

                                        #region Tính Overtime
                                        if (_ts1.TotalMinutes > 30)
                                        {
                                            if (_TimeIn2 >= _NightStart && _TimeIn2 <= _NightEnd)
                                            {
                                                if (_CheckIn2 >= _NightStart && _CheckIn2 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += 0;
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_TimeIn2 - _CheckIn2).TotalHours);
                                                }
                                                else if (_CheckIn2 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _CheckIn2).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_TimeIn2 - _NightStart).TotalHours);
                                                }
                                            }
                                            else if (_TimeIn2 > _NightEnd)
                                            {
                                                if (_CheckIn2 >= _NightStart && _CheckIn2 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_TimeIn2 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _CheckIn2).TotalHours);
                                                }
                                                else if (_CheckIn2 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _CheckIn2).TotalHours + (_TimeIn2 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                                }
                                                else if (_CheckIn2 < _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_TimeIn2 - _CheckIn2).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }

                                            }
                                            else if (_TimeIn2 < _NightStart)
                                            {
                                                _MWInfo.OverTime += Convert.ToDecimal((_TimeIn2 - _CheckIn2).TotalHours);
                                                _MWInfo.NightOverTime += 0;
                                            }
                                        }

                                        _ts1 = _CheckOut2 - _TimeOut2;
                                        if (_ts1.TotalMinutes > 30)
                                        {
                                            if (_CheckOut2 >= _NightStart && _CheckOut2 <= _NightEnd)
                                            {
                                                if (_TimeOut2 >= _NightStart && _TimeOut2 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += 0;
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_CheckOut2 - _TimeOut2).TotalHours);
                                                }
                                                else if (_TimeOut2 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _TimeOut2).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_CheckOut2 - _NightStart).TotalHours);
                                                }
                                                else if (_TimeOut2 > _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut2 - _TimeOut2).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }
                                            }
                                            else if (_CheckOut2 > _NightEnd)
                                            {
                                                if (_TimeOut2 >= _NightStart && _TimeOut2 <= _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut2 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _TimeOut2).TotalHours);
                                                }
                                                else if (_TimeOut2 < _NightStart)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_NightStart - _TimeOut2).TotalHours + (_CheckOut2 - _NightEnd).TotalHours);
                                                    _MWInfo.NightOverTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                                }
                                                else if (_TimeOut2 > _NightEnd)
                                                {
                                                    _MWInfo.OverTime += Convert.ToDecimal((_CheckOut2 - _TimeOut2).TotalHours);
                                                    _MWInfo.NightOverTime += 0;
                                                }
                                            }
                                            else if (_CheckOut2 < _NightStart)
                                            {
                                                _MWInfo.OverTime += Convert.ToDecimal((_CheckOut2 - _TimeOut2).TotalHours);
                                                _MWInfo.NightOverTime += 0;
                                            }
                                        }
                                        #endregion

                                        #region Tính NightShift
                                        if (_TimeIn2 >= _NightStart && _TimeIn2 <= _NightEnd)
                                        {
                                            if (_TimeOut2 <= _NightEnd)
                                                _MWInfo.NightShiftTime += Convert.ToDecimal((_TimeOut2 - _TimeIn2).TotalHours);
                                            else if (_TimeOut2 > _NightEnd)
                                                _MWInfo.NightShiftTime += Convert.ToDecimal((_NightEnd - _TimeIn2).TotalHours);

                                        }
                                        else if (_TimeIn2 < _NightStart)
                                        {
                                            if (_TimeOut2 >= _NightStart && _TimeOut2 <= _NightEnd)
                                                _MWInfo.NightShiftTime += Convert.ToDecimal((_TimeOut2 - _NightStart).TotalHours);
                                            else if (_TimeOut2 > _NightEnd)
                                                _MWInfo.NightShiftTime += Convert.ToDecimal((_NightEnd - _NightStart).TotalHours);
                                        }
                                        else
                                            _MWInfo.NightShiftTime += 0;
                                        #endregion

                                        #endregion

                                        _MWInfo.IsWorking = true;
                                         if (!_wcfMarkWork.MarkWork_CheckExistEmployee(_EmployeeID, _currDate))
                                        {
                                            _MWInfo.ID = _wcfMarkWork.MarkWork_Insert(_MWInfo);
                                        }
                                        else //Update:
                                        {
                                            _wcfMarkWork.MarkWork_Update(_MWInfo);
                                        }
                                          
                                        # endregion

                                    }
                                }*/
                                #endregion
                            }
                        }
                    }
                }
            }
            dgvList.AutoStretchColumnsToFitWidth = true;
            dgvList.AutoSizeCells();
            return true;
        }

        private ShiftInfo GetShift(DataRow drShift)
        {
            _ShiftInfo = new ShiftInfo();
            _ShiftInfo.ID = Convert.ToInt32(drShift["Shift_ID"]);
            _ShiftInfo.Name = drShift["Shift_Name"].ToString();
            _ShiftInfo.TimeIn = drShift["Shift_TimeIn"].ToString();
            _ShiftInfo.TimeOut = drShift["Shift_TimeOut"].ToString();
            _ShiftInfo.TimeInB = drShift["Shift_TimeInB"].ToString();
            _ShiftInfo.TimeOutB = drShift["Shift_TimeOutB"].ToString();
            _ShiftInfo.Description = drShift["Shift_Description"].ToString();
            return _ShiftInfo;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (_DepartmentID == 0)
                MessageBox.Show(UpdateUI.GetString("banchuachonphongban"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                OpenSplash();
            }
        }

        private void frmMarkWork_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
        }

        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                dgvList.Selection.Focus(dgvList.PositionAtPoint(new Point(e.X, e.Y)), true);
        }

        private void mnuCellEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.Selection.ActivePosition.Row > 1 && dgvList.Selection.ActivePosition.Column > 0)
            {
                string _checkTime = "";
                string _checkID = "";
                string _CardNo = dgvList[dgvList.Selection.ActivePosition.Row, 0].Tag.ToString();
                if (dgvList[dgvList.Selection.ActivePosition.Row, dgvList.Selection.ActivePosition.Column].Value != null)
                {
                    _checkTime = dgvList[dgvList.Selection.ActivePosition.Row, dgvList.Selection.ActivePosition.Column].Value.ToString();
                    _checkID = dgvList[dgvList.Selection.ActivePosition.Row, dgvList.Selection.ActivePosition.Column].Tag.ToString();
                }
                else
                    _checkID = "0";

                frmCheckTimeEdit f = new frmCheckTimeEdit(_checkID, _CardNo);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    //Load dữ liệu sau khi cập nhật:
                    OpenSplash();
                }
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (_DepartmentID == 0)
                MessageBox.Show(UpdateUI.GetString("banchuachonphongban"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                frmViewAttendance f = new frmViewAttendance(_BranchID, _DepartmentID, dtpdate.Value);
                f.ShowDialog();
            }
        }

        private void dgvList_DoubleClick(object sender, EventArgs e)
        {
            mnuCellEdit_Click(sender, e);
        }
    }
}
